XMLHttpRequest not working, trying to test database connection [closed]

Posted by Frederick Marcoux on Pro Webmasters See other posts from Pro Webmasters or by Frederick Marcoux
Published on 2012-03-25T18:01:18Z Indexed on 2012/03/25 23:41 UTC
Read the original article Hit count: 262

Filed under:
|
|
|

I'm currently creating my own CMS for personnal use but I'm blocked at a code. I'm trying to make a installation script but the AJAX request to test if database works, doesn't work...

There's my JS code:

function testDB()
{
    "use strict";

    var host = document.getElementById('host').value;
    var username = document.getElementById('username').value;
    var password = document.getElementById('password').value;
    var db = document.getElementById('db_name').value;

    var xmlhttp = new XMLHttpRequest();
    var url = "test_db.php";
    var params = "host="+host+"&username="+username+"&password="+password+"&db="+db;
    xmlhttp.open("POST", url, true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(params);
    $('#loader').removeAttr('style');

    if (xmlhttp.responseText !== '')
    {
        if (xmlhttp.readyState===4 && xmlhttp.status===200)
        {
            $('#next').removeAttr('disabled');
            $('#test').attr('disabled', 'disabled');
            $('#test').text('Connection Successful!');
            $('#test').addClass('btn-success');
            $('#login').addClass('success');
            $('#login1').addClass('success');
            $('#db').addClass('success');
            $('#loader').attr('style', 'display: none;');
        }
        else
        {
        $('#next').attr('disabled', 'disabled');
        $('#test').removeClass('btn-success');
        $('#test').removeAttr('disabled');
        $('#test').text('Test Connection');
        $('#login').removeClass('success');
        $('#login1').removeClass('success');
        $('#db').removeClass('success');
        $('#loader').attr('style', 'display: none;');
        }
    }
    else
    {
        $('#next').attr('disabled', 'disabled');
        $('#next').attr('disabled', 'disabled');
        $('#test').removeClass('btn-success');
        $('#test').removeAttr('disabled');
        $('#test').text('Test Connection');
        $('#login').removeClass('success');
        $('#login1').removeClass('success');
        $('#db').removeClass('success');
        $('#loader').attr('style', 'display: none;');
    }
}

And there's my PHP code:

<?php
$link = mysql_connect($_POST['host'], $_POST['username'], $_POST['password']);

if (!$link)
{
    echo '';
}
else
{
    if (mysql_select_db($_POST['db']))
    {
        echo 'Connection Successful!';
    }
    else
    {
        echo '';
    }
}

mysql_close($link);
?>

I don't know why it doesn't work but I tried with JQuery $.ajax, $.get, $.post but nothing work...

© Pro Webmasters or respective owner

Related posts about php

Related posts about mysql